// JavaScript Document
var w_uid;
var w_email;
var uname;
$(function(){
	w_uid=$("#w_uid").val();
	w_email=$("#w_email").val();
	uname=$("#uname").val();
	
	if(w_uid == "") {
		alert("请先登录!");
		location.href="../login/login.html";
	}
	
	showNone();
	showTextarea();
	reply();
	comment();
});

function showNone() {
	var s=$(".show_comment ul").html();
	if($.trim(s)=="") {
		var p='<li><p style="font-size:12px;color:#333;text-align:center;';
		p+='width:100%;height:50px;line-height:50px;">暂无数据</p></li>';
		$(".show_comment ul").html(p);
	}
}

function getData(){
	$.post("../json/moodsAction_commentInfo",{"comment.objid":$("#mid").val()},function(data){
		if(data.result > 0){
			location.href="commentMood.jsp";
		}else{
			alert("系统错误!");
		}
	});
} 

/*评论、回复区域*/
function showTextarea(){
	$(".comment_btn").click(function(){
		$("#one_mood_comment").select();
		$(document).scrollTop($(document).height());
	});
	
	var rs=$(".reply_comment_btn");
	var r_area=$(".reply_commnet_area");
	$.each(rs,function(index){
		$(rs[index]).click(function(){
			$(r_area[index]).css("display","block").animate({height:'60px'},500);
			$(r_area[index]).find("textarea.reply_comment_textarea").select();
		});
	});
}

function closeReply(val) {
	var r_area=$(".reply_commnet_area");
	$(r_area[val]).animate({height:'0px'},500).css("display","none");
}

/*回复*/
function reply(){
	var replys_area=$(".reply_commnet_area");
	var reply_btns=$(".reply_btn");
	var cids=$(".cid");
	var cid=0;
	var rcontent="";
	var rtime=currentTime();
	
	$.each(reply_btns,function(i){
		$(reply_btns[i]).click(function(){
			cid=$(cids[i]).val();
			rcontent=$($(".reply_comment_textarea")[i]).val();
			if(checkInfo(rcontent) && rcontent != ""){
				$.post("../json/moodsAction_addReply", {
					"reply.cid":cid,
					"reply.rcontent":rcontent,
					"reply.rtime":rtime,
					"reply.w_uid":w_uid,
					"reply.temp":uname
				}, function(data){
					data=parseInt($.trim(data.result));
					if(data==1){
						getData();
					}else{
						alert("系统错误!");
					}
				});
			} else {
				alert("输入有误!");
			}
		});
	});
}

/*评论*/
function comment(){
	var mid=$("#mid").val();
	var ccontent = "";
	var ctime=currentTime();
	
	var reply_btn=$(".reply_comment_btn");
	$(".comment_btn").click(function(){
		$(".comment_area").css("display","block").animate({height:'100px'},300);
		$("#one_mood_comment").select();
	});
	
	$("#comment_btn").click(function(){
		ccontent=$("#one_mood_comment").val();
		if(ccontent != "" && checkInfo(ccontent)){
			$.post("../json/moodsAction_addComment", {
				"comment.w_uid":w_uid,
				"comment.ccontent":ccontent,
				"comment.ctime":ctime,
				"comment.area_id":'4',
				"comment.objid":mid,
				"comment.temp":uname
			},function(data){
				if(data.result>0){
					getData();
				}else{
					alert("系统错误!");
				}
			},"json");
		} else {
			alert("输入有误!");
		}
	});
}

/*当前时间*/
function currentTime(){
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth()+1;
	var ri = date.getDate();
	var hour = date.getHours();
	var min = date.getMinutes();
	if(month<10){
		month="0"+month;
	}
	if(ri<10){
		ri="0"+ri;
	}
	var d = year+"-"+month+"-"+ri+" "+hour+":"+min;
	return d;
}

function checkInfo(s) { 
	var result= s.replace(/<script.*?>.*?<\/script>/ig, ''); 
	if(result == ""){
		return false;
	}
	return true;
}
